package repository

import (
	"blog_go/internal/model"
	"errors"
)

type CollectionRepository interface {
	QueryCollection(userId int) ([]*model.Collection, error)
	AddCollection(collection model.Collection) error
	UpdateCollection(collection model.Collection) error
	DelCollection(collectionId int) error
}

type collectionRepository struct {
	*BaseRepository
}

func NewCollectionRepository(repository *BaseRepository) CollectionRepository {
	return &collectionRepository{
		BaseRepository: repository,
	}
}

// 查询收藏夹
func (r *collectionRepository) QueryCollection(userId int) ([]*model.Collection, error) {
	collections := make([]*model.Collection, 0)

	rows, err := r.db.Query(`
		SELECT 
			collection_id, 
			user_id, 
			collection_name, 
			COALESCE(collection_describe, '') 'collectionDescribe' 
		FROM 
			collection 
		WHERE 
			user_id = ?`, userId)
	if err != nil {
		return nil, err
	}

	defer rows.Close()

	for rows.Next() {
		var collection model.Collection

		err := rows.Scan(&collection.CollectionId, &collection.UserId, &collection.CollectionName, &collection.CollectionDescribe)
		if err != nil {
			return nil, err
		}

		collections = append(collections, &collection)
	}

	return collections, nil
}

// 添加收藏夹
func (r *collectionRepository) AddCollection(collection model.Collection) error {
	//收藏夹名不能重复
	var count int

	err := r.db.QueryRow(`
		SELECT 
			count(*) 
		FROM 
			collection 
		WHERE 
			collection_name = ? 
		AND user_id = ?`, collection.CollectionName, collection.UserId).Scan(&count)
	if err != nil {
		return err
	}

	if count > 0 {
		return errors.New("收藏夹名重复")
	}

	_, err = r.db.Exec(`
		INSERT INTO collection
		(
			collection_name, 
			user_id, 
			collection_describe
		) 
		VALUES (?, ?, ?)`, collection.CollectionName, collection.UserId, collection.CollectionDescribe)
	if err != nil {
		return err
	}

	return nil
}

// 更新收藏夹
func (r *collectionRepository) UpdateCollection(collection model.Collection) error {
	//收藏夹名不能重复
	var count int

	err := r.db.QueryRow(`
		SELECT 
			count(*) 
		FROM 
			collection 
		WHERE 
			collection_name = ? 
		AND user_id = ?
		AND collection_id != ?`, collection.CollectionName, collection.UserId, collection.CollectionId).Scan(&count)
	if err != nil {
		return err
	}

	if count > 0 {
		return errors.New("收藏夹名重复")
	}

	_, err = r.db.Exec(`
		UPDATE 
			collection 
		SET 
			collection_name = ?, 
			collection_describe = ? 
		WHERE 
			collection_id = ?`, collection.CollectionName, collection.CollectionDescribe, collection.CollectionId)
	if err != nil {
		return err
	}

	return nil
}

// 删除收藏夹
func (r *collectionRepository) DelCollection(collectionId int) error {
	//如果收藏夹下有内容也要删除
	tx, err := r.db.Begin()
	if err != nil {
		return err
	}

	defer func() {
		if err != nil {
			tx.Rollback()
		}
	}()

	//删除收藏文章
	_, err = tx.Exec(`
		DELETE FROM article_collection 
		WHERE 
			collection_id = ?`, collectionId)
	if err != nil {
		return err
	}

	_, err = tx.Exec(`
		DELETE FROM collection 
		WHERE 
			collection_id = ?`, collectionId)
	if err != nil {
		return err
	}

	err = tx.Commit()
	return err
}
